package com.molyfun.parents.im.chatrow;

import android.content.Context;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.im.ChatMessageList;
import com.molyfun.parents.im.model.ChatMessageEntity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.uievent.EventDispatcherEnum;
import com.molyfun.parents.utils.UIUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChatRowAppraise extends BaseChatRow implements View.OnClickListener {

//    private TextView contentView;

    /**
     * bigurl : http://media.molyfun.com/mf/product/pics/ic_20170629-150232985.jpg?imageMogr2/thumbnail/1280x1280
     * id : 402883e05ea869c9015ea86ca81a0000
     * planId : 691efe6342e647b3aa91aad92715781e
     * userId : e548cebc5df0285e015df453bfd908a5
     * userName : 预约咨询
     * userType : 2
     * headimgurl : 头像  http://media.molyfun.com/helloimg/user/20170907/201709071903384cJQnTjk.jpg?imageMogr2/thumbnail/300x300
     * content :    文字内容
     * url : http://media.molyfun.com/mf/product/pics/ic_20170629-150232985.jpg?imageMogr2/thumbnail/300x300
     * type : 5     记录类型   1:图片 2.视频 3.音频 4.文字 5.套餐  6.评价
     * timelen :    音频时长
     * cover :  视频封面
     * extendfield1 : 8a2d92635ceedfa6015cf2a889640b52  扩展字段1  （type=5时表示套餐Id  ；type=6时表示评价结果  1 满意  2 一般  3 不满意）
     * extendfield2 : 套餐    扩展字段2  套餐标题
     * extendfield3 : 2.00      扩展字段3  价格
     * extendfield4 : 0.10      扩展字段4  折扣价
     * extendfield5 : 2     扩展字段5  售出量
     * extendfield6 : 0     扩展字段6  库存
     * createdatetimeStr : 2017-09-22 15:10:47
     */

    private TextView mAppraiseGeneral;
    private TextView mAppraiseSatisfaction;
    private TextView mAppraiseUnsatisfactory;
    private String TAG = "ChatRowAppraise";
    protected ChatMessageList.MessageListItemListener itemClickListener;

    public ChatRowAppraise(Context context, ChatMessageEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
        inflater.inflate(R.layout.ease_row_received_appraise, this);
    }

    @Override
    protected void onFindViewById() {
        mAppraiseGeneral = (TextView) findViewById(R.id.appraise_general);
        mAppraiseSatisfaction = (TextView) findViewById(R.id.appraise_satisfaction);
        mAppraiseUnsatisfactory = (TextView) findViewById(R.id.appraise_unsatisfactory);

        System.out.println("onFindViewById--->" + message.toString());

        if (!TextUtils.isEmpty(message.getExtendfield1()) && (message.getExtendfield1().equals("1") || message.getExtendfield1().equals("2") || message.getExtendfield1().equals("3"))) {
            mAppraiseSatisfaction.setVisibility(View.INVISIBLE);
            mAppraiseUnsatisfactory.setVisibility(View.INVISIBLE);
            if (message.getExtendfield1().equals("1")) {
                mAppraiseGeneral.setText("满意");
            } else if (message.getExtendfield1().equals("2")) {
                mAppraiseGeneral.setText("一般");
            } else if (message.getExtendfield1().equals("3")) {
                mAppraiseGeneral.setText("不满意");
            }
        } else {
            mAppraiseGeneral.setOnClickListener(this);
            mAppraiseSatisfaction.setOnClickListener(this);
            mAppraiseUnsatisfactory.setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.appraise_general:
                appraise("2");
                break;
            case R.id.appraise_satisfaction:
                appraise("1");
                break;
            case R.id.appraise_unsatisfactory:
                appraise("3");
                break;
        }
    }

    private void appraise(final String degree) {
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().postCustomplanEval(message.getCustomplanid(), degree, message.getId());
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                if (response.body() != null) {
                    if (response.body().getCode() == 200) {
                        UIUtils.showToast(getContext(), "评价成功");
                        Message uimessage = new Message();
                        uimessage.what = EventDispatcherEnum.UI_EVENT_CHAT_FINISH;
                        uimessage.obj = message.getCustomplanid();
                        MFPApplication.getInstance().getEventDispatcher().sendMessage(uimessage);
                        mAppraiseSatisfaction.setVisibility(View.INVISIBLE);
                        mAppraiseUnsatisfactory.setVisibility(View.INVISIBLE);
                        if (degree.equals("1")) {
                            mAppraiseGeneral.setText("满意");
                        } else if (degree.equals("2")) {
                            mAppraiseGeneral.setText("一般");
                        } else if (degree.equals("3")) {
                            mAppraiseGeneral.setText("不满意");
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
            }
        });
    }

    @Override
    public void onSetUpView() {
        handleTextMessage();
        System.out.println("onSetUpView--->" + message.toString());
        if (!TextUtils.isEmpty(message.getExtendfield1()) && (message.getExtendfield1().equals("1") || message.getExtendfield1().equals("2") || message.getExtendfield1().equals("3"))) {
            mAppraiseSatisfaction.setVisibility(View.INVISIBLE);
            mAppraiseUnsatisfactory.setVisibility(View.INVISIBLE);
            if (message.getExtendfield1().equals("1")) {
                mAppraiseGeneral.setText("满意");
            } else if (message.getExtendfield1().equals("2")) {
                mAppraiseGeneral.setText("一般");
            } else if (message.getExtendfield1().equals("3")) {
                mAppraiseGeneral.setText("不满意");
            }
        } else {
            mAppraiseGeneral.setOnClickListener(this);
            mAppraiseSatisfaction.setOnClickListener(this);
            mAppraiseUnsatisfactory.setOnClickListener(this);
        }
    }

    protected void handleTextMessage() {
//        progressBar.setVisibility(View.GONE);
//        statusView.setVisibility(View.GONE);
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onBubbleClick() {
        // TODO Auto-generated method stub
    }

}
